###############################################################
# Replication Code for 'Bridging Spatial and Saliency Theory' #
#                                                             #
# Chitralekha Basu, 2018                                      #
###############################################################

rm(list=ls())

sink("main_analysis.txt")

data <- read.csv("data_bsas.csv") 

library(lfe)
library(survival)
library(interplot)


########################################################
#       PREPARING DATASET FOR BUC ESTIMATION           #
########################################################

data1 <- data
data2 <- data
data3 <- data

data1$bucsal_ches <- ifelse(data1$salience_ches>=3,1,0)

data2$bucsal_ches <- ifelse(data2$salience_ches>=2,1,0)

data3$bucsal_ches <- ifelse(data3$salience_ches>=1,1,0)

bucpooled <- rbind(data1,data2,data3)


########################################################
#                     TABLE 1                          #
########################################################

# Model 1
sext <- felm(partyext ~ supporterext:major
             + supporterext 
             + major:envt       
             + major:EU           
             + major:immig           
             + major:spendvtax  
             + major:redistribution          
             + major:ssm        
             + major:stint
             + major:civlib
             | country.issue | 0 | country.issue,
             exactDOF=T,
             data)

summary(sext)
nrow(sext$fitted.values) # sample size

# Model 2
sext2 <- felm(partyext ~ supporterext:major
             + supporterext
             + supportersd
             + govt_allyrs
             + partner_emph_ches
             + major:envt       
             + major:EU           
             + major:immig           
             + major:spendvtax  
             + major:redistribution          
             + major:ssm        
             + major:stint
             + major:civlib
             | country.issue | 0 | country.issue,
             exactDOF=T,
             data)

summary(sext2)
nrow(sext2$fitted.values)

# Model 3
smech <- clogit(bucsal_ches ~ partyext:major
                 + partyext
                 + major:envt       
                 + major:EU           
                 + major:immig           
                 + major:spendvtax  
                 + major:redistribution          
                 + major:ssm        
                 + major:stint
                 + major:civlib
                 + strata(country.issue)
                 + cluster(country.issue),
                 data=bucpooled,
                 method="efron")

summary(smech)
nrow(model.frame(smech))/3 # sample size
logLik(smech) # log likelihood 

# Model 4
smech2 <- clogit(bucsal_ches ~ partyext:major
                + partyext
                + supportersd
                + govt_allyrs
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(smech2)
nrow(model.frame(smech2))/3 
logLik(smech2) 


########################################################
#                     TABLE 2                          #
########################################################

# Model 1
clog1 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog1)
nrow(model.frame(clog1))/3 
logLik(clog1) 

# Model 2
clog2 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog2)
nrow(model.frame(clog2))/3 
logLik(clog2) 

# Model 3
clog3 <- clogit(bucsal_ches ~ supporterdist:major
                + supporterdist
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog3)
nrow(model.frame(clog3))/3 
logLik(clog3)  

# Model 4
clog4 <- clogit(bucsal_ches ~ supporterdist:major
                + supporterdist
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog4)
nrow(model.frame(clog4))/3 
logLik(clog4) 


########################################################
#                    FIGURE 1                          #
########################################################

fake <- c(seq(0,30,by=1))

coef1 <- rep(0,31)
ub <- rep(0,31)
lb <- rep(0,31)

for(i in 1:31){
  
  d <- subset(data,data$hst_avg00>fake[i]) 
 
  d1 <- d
  d2 <- d
  d3 <- d
  
  d1$bucsal_ches <- ifelse(d1$salience_ches>=3,1,0) 
  
  d2$bucsal_ches <- ifelse(d2$salience_ches>=2,1,0)
  
  d3$bucsal_ches <- ifelse(d3$salience_ches>=1,1,0)
  
  bucd <- rbind(d1,d2,d3)
  
  bucd$issue <- droplevels(bucd$issue) 
  
  m <-  clogit(bucsal_ches ~ supporterext
               + supportersd
               + partner_emph_ches
               + govt_allyrs
               + strata(country.issue)
               + cluster(country.issue),
               data=bucd,
               method="efron")
  
  coef1[i] <- exp(m$coefficients[1])
  ub[i] <- exp(m$coefficients[1]+1.96*sqrt(m$var[1,1]))
  lb[i] <- exp(m$coefficients[1]-1.96*sqrt(m$var[1,1]))
  
}

coef1 <- (coef1-1)*100
ub <- (ub-1)*100
lb <- (lb-1)*100

df <- data.frame(cbind(fake, coef1, lb, ub))

dev.new()

pdf("majthreshplot.pdf", width=14, height=10)

p <- interplot(df, point=T) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  theme_bw() +
  theme(axis.text = element_text(size=16),
        axis.title = element_text(size=18),
        plot.title = element_text(size=16)) 

p + xlab("Average Seat Share Threshold") + 
  ylab(expression(paste("% ", Delta, " Emphasis Odds Ratios per ", Delta, "Extremism"))) 

dev.off()

########################################################
#                     TABLE 3                          #
########################################################

# Model 1
clog5 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + lrgen:envt       
                + lrgen:EU           
                + lrgen:immig           
                + lrgen:spendvtax  
                + lrgen:redistribution          
                + lrgen:ssm        
                + lrgen:stint
                + lrgen:civlib
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog5)
nrow(model.frame(clog5))/3 
logLik(clog5) 

# Model 2
clog6 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext
                + supportersd
                + govt_allyrs
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + ptyfam.cmp
                + fam_emph_ches
                + partner_emph_ches
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog6)
nrow(model.frame(clog6))/3 
logLik(clog6) 

# creating dummy variables for major mst, major niche, minor mst and minor niche parties
# classing all parties considered by either Meguid (2005) or Adams et al. (2006) as mst, remainder niche

bucpooled$niche.flex.major <- bucpooled$major*bucpooled$niche.flex
bucpooled$mst.flex.major <- bucpooled$major*bucpooled$mst.flex
bucpooled$niche.flex.minor <- bucpooled$minor*bucpooled$niche.flex
bucpooled$mst.flex.minor <- bucpooled$minor*bucpooled$mst.flex

# Model 3
clog7 <- clogit(bucsal_ches ~ supporterext:mst.flex.major
                + supporterext:niche.flex.minor
                + supporterext:niche.flex.major
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches 
                + mst.flex.major:envt       
                + mst.flex.major:EU           
                + mst.flex.major:immig           
                + mst.flex.major:spendvtax  
                + mst.flex.major:redistribution          
                + mst.flex.major:ssm        
                + mst.flex.major:stint
                + mst.flex.major:civlib
                + niche.flex.major:envt       
                + niche.flex.major:EU           
                + niche.flex.major:immig           
                + niche.flex.major:spendvtax  
                + niche.flex.major:redistribution          
                + niche.flex.major:ssm        
                + niche.flex.major:stint
                + niche.flex.major:civlib
                + niche.flex.minor:envt       
                + niche.flex.minor:EU           
                + niche.flex.minor:immig           
                + niche.flex.minor:spendvtax  
                + niche.flex.minor:redistribution          
                + niche.flex.minor:ssm        
                + niche.flex.minor:stint
                + niche.flex.minor:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog7)
nrow(model.frame(clog7))/3 
logLik(clog7) 

# Model 4
clog8 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext:bopla
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches 
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + bopla:envt       
                + bopla:EU           
                + bopla:immig           
                + bopla:spendvtax  
                + bopla:redistribution          
                + bopla:ssm        
                + bopla:stint
                + bopla:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog8)
nrow(model.frame(clog8))/3 
logLik(clog8) 

# Model 5
clog9 <- clogit(bucsal_ches ~ supporterext:govt_allyrs
                + supporterext:major
                + supporterext
                + supportersd
                + partner_emph_ches
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + govt_allyrs:envt       
                + govt_allyrs:EU           
                + govt_allyrs:immig           
                + govt_allyrs:spendvtax  
                + govt_allyrs:redistribution          
                + govt_allyrs:ssm        
                + govt_allyrs:stint
                + govt_allyrs:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog9)
nrow(model.frame(clog9))/3  
logLik(clog9) 

# Model 6
clog10 <- clogit(bucsal_ches ~ supporterext:major
                + supporterext:yearsinoffice
                + supporterext
                + supportersd
                + govt_allyrs
                + partner_emph_ches 
                + major:envt       
                + major:EU           
                + major:immig           
                + major:spendvtax  
                + major:redistribution          
                + major:ssm        
                + major:stint
                + major:civlib
                + yearsinoffice:envt       
                + yearsinoffice:EU           
                + yearsinoffice:immig           
                + yearsinoffice:spendvtax  
                + yearsinoffice:redistribution          
                + yearsinoffice:ssm        
                + yearsinoffice:stint
                + yearsinoffice:civlib
                + strata(country.issue)
                + cluster(country.issue),
                data=bucpooled,
                method="efron")

summary(clog10)
nrow(model.frame(clog10))/3 
logLik(clog10) 

sink()